﻿Public Class Empresas 
    Public Sub Mostrar()
        If guardar.Enabled = False Then
            guardar.Enabled = True
            cancelar.Enabled = True
            agregar.Enabled = False
            modificar.Enabled = False
            eliminar.Enabled = False
            imprimir.Enabled = False
            PanelArriba.Enabled = True
            PanelAbajo.Enabled = False
        Else
            cancelar.Enabled = False
            guardar.Enabled = False
            agregar.Enabled = True
            Modificar.Enabled = True
            Eliminar.Enabled = True
            imprimir.Enabled = True
            PanelArriba.Enabled = False
            PanelAbajo.Enabled = True

        End If
    End Sub
    Private Sub EmpresasBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EmpresasBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.EmpresasBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.DbFireAndIceDataSet)

    End Sub

    Private Sub Empresas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.tipospago' table. You can move, or remove it, as needed.
        Me.TipospagoTableAdapter.Fill(Me.DbFireAndIceDataSet.tipospago)
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.empresas' table. You can move, or remove it, as needed.
        Me.EmpresasTableAdapter.Fill(Me.DbFireAndIceDataSet.empresas)

    End Sub

    Private Sub agregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles agregar.Click
        BindingNavigatorAddNewItem.PerformClick()
        IsactiveCheckBox.CheckState = CheckState.Checked
        FacturarhoraespecialCheckBox.CheckState = CheckState.Unchecked
        Mostrar()
    End Sub

    Private Sub modificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles modificar.Click
        Mostrar()
    End Sub

    Private Sub eliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles eliminar.Click
        Dim opcion As Integer
        opcion = MsgBox("¿Está seguro que desea eliminar el registro?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Eliminar registro")
        If opcion = vbYes Then
            Try
                EmpresasTableAdapter.DeleteQuery(IdempresaTextBox.Text)
                Me.EmpresasTableAdapter.Fill(Me.DbFireAndIceDataSet.empresas)
            Catch ex As Exception
                MsgBox("Lo sentimos, el registro no puede ser eliminado ya que está relacionado con otras tablas", MsgBoxStyle.Critical, "Error")
            End Try


        End If
    End Sub

    Private Sub cancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancelar.Click

        ErrorProvider1.SetError(HorafinalTextEdit, Nothing)
        ErrorProvider1.SetError(HorainicioTextEdit, Nothing)
        ErrorProvider1.SetError(IdtipopagoTextEdit, Nothing)

        ErrorProvider1.SetError(DescripcionTextBox, Nothing)
        ErrorProvider1.SetError(MontosubsidioTextBox, Nothing)
        ErrorProvider1.SetError(Telefono1TextBox, Nothing)
        ErrorProvider1.SetError(IsactiveCheckBox, Nothing)
        Mostrar()
        Me.EmpresasTableAdapter.Fill(Me.DbFireAndIceDataSet.empresas)
    End Sub

    Private Sub guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles guardar.Click

        ''Valida que los campos si tiene restricciones especiales a la hora de facturar
        If FacturarhoraespecialCheckBox.CheckState = CheckState.Checked Then

            If String.IsNullOrEmpty(HorainicioTextEdit.Text) Then
                ErrorProvider1.SetError(HorainicioTextEdit, "Debe ingresar una hora de inicio")
                Return
            End If
            ErrorProvider1.SetError(HorainicioTextEdit, Nothing)
            If String.IsNullOrEmpty(HorafinalTextEdit.Text) Then
                ErrorProvider1.SetError(HorafinalTextEdit, "Debe ingresar una hora final")
                Return
            End If
            ErrorProvider1.SetError(HorafinalTextEdit, Nothing)
            If HorainicioTextEdit.EditValue >= HorafinalTextEdit.EditValue Then
                ErrorProvider1.SetError(HorainicioTextEdit, "La hora de inicio debe ser menor a la hora final")
                Return
            End If
            ErrorProvider1.SetError(HorainicioTextEdit, Nothing)
            If (String.IsNullOrEmpty(IdtipopagoTextEdit.Text)) Then
                ErrorProvider1.SetError(IdtipopagoTextEdit, "Debe seleccionar un tipo de pago")
                Return
            End If
            ErrorProvider1.SetError(IdtipopagoTextEdit, Nothing)
        End If



        If (DescripcionTextBox.Text = "") Then
            ErrorProvider1.SetError(DescripcionTextBox, "Debe ingresar un nombre")
            Return
        End If
        ErrorProvider1.SetError(DescripcionTextBox, Nothing)



        If (MontosubsidioTextBox.Text = "") Then
            ErrorProvider1.SetError(MontosubsidioTextBox, "Debe ingresar una cantidad")
            Return
        End If
        ErrorProvider1.SetError(MontosubsidioTextBox, Nothing)



        'If Telefono1TextBox.Text.Length < 8 Then
        '    ErrorProvider1.SetError(Telefono1TextBox, "Debe ingresar un número mayor a 8 dígitos")
        '    Return
        'End If

        'ErrorProvider1.SetError(Telefono1TextBox, Nothing)

        'If ((DescripcionTextBox.Text <> "") And (MontosubsidioTextBox.Text <> "") And (Telefono1TextBox.Text <> "")) And (IsactiveCheckBox.CheckState <> CheckState.Indeterminate) Then

        '    ErrorProvider1.SetError(DescripcionTextBox, Nothing)
        '    ErrorProvider1.SetError(MontosubsidioTextBox, Nothing)
        '    ErrorProvider1.SetError(Telefono1TextBox, Nothing)
        '    ErrorProvider1.SetError(IsactiveCheckBox, Nothing)
        Try
            EmpresasBindingNavigatorSaveItem.PerformClick()
            Mostrar()

        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Error", "Nose pudo guardar el registro")

        End Try
        AlertControl1.AppearanceCaption.ForeColor = Color.Green
        AlertControl1.Show(Me, "Advertencia", "Se guardó el resgistro satisfactoriamente")
        'End If



    End Sub

    Private Sub imprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imprimir.Click
        GridControl1.ShowPrintPreview()

    End Sub


    Private Sub Telefono2TextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If ((e.KeyChar <> Chr(8)) And (e.KeyChar <= Chr(47)) Or (e.KeyChar >= Chr(58))) Then
            e.Handled = True
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Error", "Solo puede ingresar números")
            Telefono2TextBox.Focus()

        ElseIf Char.IsSeparator(e.KeyChar) Then
            e.Handled = True
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Error", "No debe ingresar espacios")
            Telefono2TextBox.Focus()

        Else
            e.Handled = False

        End If
    End Sub



    Private Sub Telefono2TextBox_KeyPress_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Telefono2TextBox.KeyPress
        If Char.IsLetter(e.KeyChar) Then
            e.Handled = True
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Advertencia", "Solo puede ingresar números")
            Telefono1TextBox.Focus()

        ElseIf Char.IsSeparator(e.KeyChar) Then
            e.Handled = True
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Advertencia", "No debe ingresar espacios")
            Telefono1TextBox.Focus()

        Else
            e.Handled = False

        End If
    End Sub

    Private Sub Telefono1TextBox_KeyPress_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Telefono1TextBox.KeyPress
        If ((e.KeyChar <> Chr(8)) And (e.KeyChar <= Chr(47)) Or (e.KeyChar >= Chr(58))) Then
            e.Handled = True
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Advertencia", "Solo puede ingresar números")
            Telefono1TextBox.Focus()

        ElseIf Char.IsSeparator(e.KeyChar) Then
            e.Handled = True
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Advertencia", "No debe ingresar espacios")
            Telefono1TextBox.Focus()

        Else
            e.Handled = False

        End If
    End Sub

    Private Sub MontosubsidioTextBox_KeyPress_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MontosubsidioTextBox.KeyPress
        If Char.IsLetter(e.KeyChar) Then
            e.Handled = True
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Advertencia", "Solo puede ingresar números")
            Telefono1TextBox.Focus()

        ElseIf Char.IsSeparator(e.KeyChar) Then
            e.Handled = True
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Advertencia", "No debe ingresar espacios")
            Telefono1TextBox.Focus()

        Else
            e.Handled = False

        End If
    End Sub

    Private Sub CorreoelectronicoTextBox_Leave_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CorreoelectronicoTextBox.Leave
        Dim strCorreo As String
        strCorreo = CorreoelectronicoTextBox.Text
        Dim blnCorreo As Boolean
        Dim rgx As New System.Text.RegularExpressions.Regex("\w+([-+.´]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")
        If rgx.IsMatch(strCorreo) Then
            blnCorreo = True

        Else
            blnCorreo = False
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Advertencia", "El Formato de correo electrónico no es válido")
            CorreoelectronicoTextBox.Focus()
        End If
    End Sub


End Class